package cn.com.zs.android.view;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import cn.com.zs.android.R;
import cn.com.zs.android.model.GameParams;
import cn.com.zs.android.model.PhoneParams;
import cn.com.zs.android.service.BackGroundMusicService;
import cn.com.zs.android.util.Constants;
import cn.com.zs.android.util.HolePlace;
import cn.com.zs.android.util.Image;
import cn.com.zs.android.util.MusicPlayerUtils;
import cn.com.zs.android.util.NumberUtil;
import cn.com.zs.android.util.StringUtils;
import cn.com.zs.android.util.VoiceSettings;

/**
 * 绘图管理（负责绘画的主要逻辑）
 * @author 曾帅
 */
public class DrawImageManager {
	
	private MusicPlayerUtils musicPlayer;
	/** 当前哪些洞出小鱼 **/
	private static int[] WHICH_HOLE_COME_OUT;
	/** 每个洞小鱼的状态 **/
	private static int[] EACH_HOLE_STAUTS;
	/** 被击中的小鱼 **/
	private static boolean[] WHICH_HAMSTER_HIT;
	/** 被强制为空洞的洞洞 **/
	private static boolean[] WHICH_HLOE_EMPTY;
	/** 是否击中 **/
	private static boolean isHit = false;
	
	/**
	 * 绘图
	 * @param image
	 */
	public void draw(Image image, boolean isResetDate) {
		if (isResetDate) {
			// 重置数据
			this.resetDate();
		}
		// 如果出现了触击屏幕的动作并且击中了
		if(isHit) {
			// 击中了, 加分
			for (int i = 0; i < WHICH_HAMSTER_HIT.length; i ++) {
				if (WHICH_HAMSTER_HIT[i]) {
					WHICH_HAMSTER_HIT[i] = false;
					GameParams.score ++;
				}
			}
		}
		
		try {
			// 绘下一张图
			image.DrawImage(WHICH_HOLE_COME_OUT, EACH_HOLE_STAUTS);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 如果地洞状态大于最终状态, 则取消该洞
		for (int i = 0; i < EACH_HOLE_STAUTS.length; i ++) {
			if (EACH_HOLE_STAUTS[i] == Constants.HAMSTER_STATUS_HIT) {
				this.cancelEmptyHoleByNumber(i);
			}
		}
		isHit = false;
	}
	
	public void touch(Context context, float x, float y) {
		for (int i = 0; i < WHICH_HOLE_COME_OUT.length; i ++) {
			int[] holePlace = HolePlace.getDesignationPlace(WHICH_HOLE_COME_OUT[i]);
			if(x > holePlace[0] && x < holePlace[2] && y > holePlace[1] && y < holePlace[3]){
				if (this.isCanHitStauts(i)) {
					// 播放小鱼挨揍音效
					if(GameParams.isSystemSound) {
						if(musicPlayer == null) {
							musicPlayer = new MusicPlayerUtils();
						}
						musicPlayer.playMusic(context, R.raw.hamster_cry);
					}
					
					isHit = true;
					WHICH_HAMSTER_HIT[i] = true;
					EACH_HOLE_STAUTS[i] = Constants.HAMSTER_STATUS_HIT;
				} else {
					// 如果小鱼出现状态不对, 视为未击中, 减分。
//					GameParams.score --;
				}
			}
		}
	}

	/**
	 * 音效 设置
	 */
	public void voiceSettings(Context context, float x, float y) {
		int[] background_music = Constants.BACKGROUND_MUSIC;
		if(x > background_music[0] && x < background_music[2] && y > background_music[1] && y < background_music[3]){
			if(GameParams.isBackgroundMusic) {
				VoiceSettings.closeBackgroundMusic();
				BackGroundMusicService.pauseMusic();
			} else {
				VoiceSettings.openBackgroundMusic();
				BackGroundMusicService.goonMusic();
			}
		}
		int[] system_sound = Constants.SYSTEM_SOUND;
		if(x > system_sound[0] && x < system_sound[2] && y > system_sound[1] && y < system_sound[3]){
			if(GameParams.isSystemSound) {
				VoiceSettings.closeSystemSound();
			} else {
				VoiceSettings.openSystemSound();
			}
		}
	}
	
	/**
	 * 往图片上写字, time和sroce
	 * @param canvas
	 */
	public void drawTimeAndScore(Canvas canvas, long gameTime) {
		Paint paint = new Paint();
		paint.setColor(Color.BLUE);
		paint.setTextSize(PhoneParams.getWidthSize(30));
		
//		canvas.drawText("Time :" + this.formatStr(String.valueOf(gameTime)) + "秒", PhoneParams.getWidthSize(300), PhoneParams.getWidthSize(50), paint);
		if (GameParams.score < 0) {
			paint.setColor(Color.RED);
		}
		canvas.drawText(this.formatStr((String.valueOf(GameParams.score))), Constants.SCORE_DISTANCE_SIDE_LEFT + PhoneParams.getWidthSize(20), PhoneParams.getWidthSize(80), paint);
	}
	
	private String formatStr(String str) {
		if (str.length() <= 1) {
			return StringUtils.fillPlace(str, 2, " ");
		}else if (str.length() <= 2) {
			return StringUtils.fillPlace(str, 1, " ");
		}
		return str;
	}
	
	/**
	 * 绘画背景
	 * @param image
	 */
	public void backgroundImage(Image image) {
		image.backgroundImage();
	}
	
	/**
	 * 绘画倒计时
	 * @param image
	 */
	public void countdownImage(Image image, long readyTime) {
		image.countdownImage(readyTime);
	}
	
	public void initDate() {
		// 随机生产1或2或3个地洞, 并存在于不同位置
		WHICH_HOLE_COME_OUT = NumberUtil.noRepeatRandomNumber(new Random().nextInt(Constants.MAX_HOLE_OUT) + 1, Constants.HOLE_TOTAL);
		WHICH_HOLE_COME_OUT = NumberUtil.addArrayElement(WHICH_HOLE_COME_OUT, 1);
		EACH_HOLE_STAUTS = new int[WHICH_HOLE_COME_OUT.length];
		WHICH_HAMSTER_HIT = new boolean[Constants.MAX_HOLE_OUT];
		WHICH_HLOE_EMPTY = new boolean[Constants.MAX_HOLE_OUT];
		for(int i = 0; i < WHICH_HOLE_COME_OUT.length; i++) {
			EACH_HOLE_STAUTS[i] = Constants.HAMSTER_STATUS_START;
			WHICH_HAMSTER_HIT[i] = false;
			WHICH_HLOE_EMPTY[i] = false;
		}
	}
	
	private void resetDate() {
		// 地洞状态更新, 取消空洞
		this.updateStautsHole();
		
		// 上一轮都有哪写洞出现小鱼
		int[] lastHole = WHICH_HOLE_COME_OUT;
		// 要出多少个小鱼
		int hamsters = Constants.MAX_HOLE_OUT - lastHole.length;
		if (hamsters > 0) {
			WHICH_HOLE_COME_OUT = NumberUtil.noRepeatRandomNumber(new Random().nextInt(hamsters) + 1, Constants.HOLE_TOTAL);
			WHICH_HOLE_COME_OUT = NumberUtil.addArrayElement(WHICH_HOLE_COME_OUT, 1);
		}
		// 把上一轮和这一轮出现的洞组合
		WHICH_HOLE_COME_OUT = NumberUtil.mergerArrayAndNoRepeat(lastHole, WHICH_HOLE_COME_OUT);
		for(int i = 0; i < WHICH_HOLE_COME_OUT.length; i++) {
			if (EACH_HOLE_STAUTS.length < i + 1) {
				EACH_HOLE_STAUTS = NumberUtil.extendArray(EACH_HOLE_STAUTS, 1, Constants.HAMSTER_STATUS_START);
			}
		}
	}
	
	private void updateStautsHole() {
		// 首先将地洞状态更新
		for (int i = 0; i < EACH_HOLE_STAUTS.length; i ++) {
			if (EACH_HOLE_STAUTS[i] != Constants.HAMSTER_STATUS_HIT) {
				EACH_HOLE_STAUTS[i] = EACH_HOLE_STAUTS[i] + 1;
			}
		}
		// 将空洞取消
		this.cancelEmptyHole();
	}
	
	private void cancelEmptyHole() {
		for (int i = 0; i < EACH_HOLE_STAUTS.length; i ++) {
			// 如果当前洞为空洞状态, 去掉这个洞
			if (EACH_HOLE_STAUTS[i] == Constants.HAMSTER_STATUS_END + 1) {
				this.cancelEmptyHoleByNumber(i);
				this.cancelEmptyHole();
			}
		}
	}
	
	private void cancelEmptyHoleByNumber(int number) {
		EACH_HOLE_STAUTS = NumberUtil.cancelElement(EACH_HOLE_STAUTS, number);
		WHICH_HOLE_COME_OUT = NumberUtil.cancelElement(WHICH_HOLE_COME_OUT, number);
	}

	/**
	 * 如果当前number不是空洞状态或击中状态, 视为 可以击中
	 * 
	 * @param number
	 * @return
	 */
	private boolean isCanHitStauts(int number) {
		if(EACH_HOLE_STAUTS[number] >= Constants.HAMSTER_STATUS_START
				&& EACH_HOLE_STAUTS[number] <= Constants.HAMSTER_STATUS_END) {
			return true;
		}
		return false;
	}
	
	public void drawGameOver(Image image) {
		image.gameOverImage();
	}
	
	public void drawReStart(Image image) {
		image.gameRestartImage();
	}
}
